/*
 * @Author: your name
 * @Date: 2021-05-10 17:57:00
 * @LastEditTime: 2021-05-11 15:49:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \zm_mis\src\lib\vue-py.js
 */
import { pinyin } from './pinyin.js'
export default {
  /* 汉字转拼音
  @param {string} l1 待转汉字
  @param {boolean} first 是否为拼音首字母
   */
  chineseToPinYin: function (l1, first) {
    const l2 = l1.length
    let I1 = ''
    // eslint-disable-next-line prefer-regex-literals
    const reg = new RegExp('[a-zA-Z0-9]')
    for (let i = 0; i < l2; i++) {
      const val = l1.substr(i, 1)
      const name = this.arraySearch(val, pinyin)
      if (reg.test(val)) {
        I1 += val
      } else if (name !== false) {
        I1 += !first ? name : name[0];
      }
    }
    I1 = I1.replace(/ /g, '-')
    while (I1.indexOf('--') > 0) {
      I1 = I1.replace('--', '-')
    }
    return I1;
  },
  arraySearch: function (l1) {
    for (const name in pinyin) {
      if (pinyin[name].indexOf(l1) !== -1) {
        return this.ucfirst(name)
      }
    }
    return false
  },
  ucfirst: function (l1) {
    if (l1.length > 0) {
      const first = l1.substr(0, 1).toUpperCase()
      const spare = l1.substr(1, l1.length)
      return first + spare
    }
  }
}
